package com.app.zenmewan.controller;

import com.app.zenmewan.commons.Actions;
import com.app.zenmewan.commons.Message;
import com.app.zenmewan.entity.User;
import com.app.zenmewan.service.UserService;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;


/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2020-05-01 18:25:19
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;
    @Resource
    private Actions actions;
    private ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
//    @Autowired
//    private Actions

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public User selectOne(Integer id) {
        return this.userService.queryById(id);
    }

    @AllArgsConstructor
    @Getter
    @Setter
    class UserVo {
        int uid;
        String nickname;
        String username;
        int level;
        String avatar_Url;
        String intro;

    }

    @GetMapping("list")
    public List<User> list() {
        List<User> list = this.userService.queryAll(new User());
        for (User user : list) {
            user.setLevel((int) Math.floor(Math.log(user.getPoint()) * 1.02));
        }
        return list;
    }

    @GetMapping("dongjie")
    public void dongjie(int id) {
        this.userService.Dongjie(id);
    }

    @GetMapping("jiedong")
    public void jiedong(int id) {
        this.userService.Jiedong(id);
    }

    @PostMapping("modify")
    public void modify(@RequestBody User user) {
        User u = userService.queryById(user.getId());
        if (user.getIntro() != null)
            u.setIntro(user.getIntro());
        u.setAvataurl(user.getAvataurl());
        u.setNickname(user.getNickname());
        userService.update(u);
    }

    @GetMapping("refresh")
    public ModelAndView refresh(String token) {
        int uid = actions.checkUid(token);
        User user = this.userService.queryById(uid);
        //配置level
        int level = (int) Math.floor(Math.log(user.getPoint()) * 1.02);
        UserVo vo = new UserVo(user.getId(), user.getNickname(), user.getUsername(), level, user.getAvataurl(), user.getIntro());
        modelAndView.addObject("user", vo);
        modelAndView.addObject("token", token);
        modelAndView.setStatus(HttpStatus.resolve(200));
        modelAndView.addObject(Message.TYPE, Message.OK_LOGIN);
        return modelAndView;
    }

    @PostMapping("login")
    public ModelAndView login(@RequestBody User res) {

        //不正确
        if (res.getUsername() == null || res.getUsername().length() < 6) {
            modelAndView.setStatus(HttpStatus.BAD_REQUEST);
            modelAndView.addObject(Message.MESS, "用户名是非法的。");
            return modelAndView;
        } else if (res.getPassword() == null || res.getPassword().length() < 6) {
            modelAndView.setStatus(HttpStatus.BAD_REQUEST);
            modelAndView.addObject(Message.MESS, "密码不正确。");
            return modelAndView;
        }
        User u = new User();
        u.setUsername(res.getUsername());
        u.setPassword(res.getPassword());
        List<User> queryList = this.userService.queryAll(u);

        //只有这行通过
        if (queryList.size() == 1) {
            User user = queryList.get(0);
            if(user.getIsdisable()==1){
                modelAndView.setStatus(HttpStatus.BAD_REQUEST);
                modelAndView.addObject(Message.MESS,"你的用户被冻结！");
                return modelAndView;
            }
            String uuid = String.valueOf(UUID.randomUUID());
            user.setToken(uuid);
            this.userService.update(user);
            //配置level
            int level = (int) Math.floor(Math.log(user.getPoint()) * 1.02);
            UserVo vo = new UserVo(user.getId(), user.getNickname(), user.getUsername(), level, user.getAvataurl(), user.getIntro());
            modelAndView.addObject("user", vo);
            modelAndView.addObject("token", uuid);
            modelAndView.setStatus(HttpStatus.resolve(200));
            modelAndView.addObject(Message.TYPE, Message.OK_LOGIN);
            return modelAndView;
        } else {
            //不通过
            modelAndView.setStatus(HttpStatus.BAD_REQUEST);
            modelAndView.addObject(Message.MESS, "用户名或密码不正确。");
            return modelAndView;
        }
    }

    @PostMapping("register")
    public ModelAndView register(@RequestBody User req) {
        try {
            User u = new User();
            u.setUsername(req.getUsername());
            if (this.userService.queryAll(u).size() > 0) {
                modelAndView.setStatus(HttpStatus.BAD_REQUEST);
                modelAndView.addObject(Message.MESS, "用户名已存在。");
                return modelAndView;
            }
            req.setPoint(3);
            req.setIsdisable(0);
            this.userService.insert(req);
            modelAndView.addObject(Message.TYPE, Message.OK);
            return modelAndView;
        } catch (Exception e) {
            modelAndView.setStatus(HttpStatus.BAD_REQUEST);
            modelAndView.addObject(Message.MESS, e.getLocalizedMessage());
            return modelAndView;
        }
    }
    @Setter
    @Getter
    class ModifyPwVo{
        int id;
        String pw1;
        String pw2;
    }
    @GetMapping("modifypw")
    public ModelAndView modifypw(int id,String pw1,String pw2){
        User u = this.userService.queryById(id);
        if(pw1.equals(u.getPassword())){
            u.setPassword(pw2);
            this.userService.update(u);
            modelAndView.setStatus(HttpStatus.OK);
            modelAndView.addObject(Message.MESS,Message.OK);
            return modelAndView;
        }else{
            modelAndView.setStatus(HttpStatus.BAD_REQUEST);
            modelAndView.addObject(Message.MESS,"密码错误");
            return modelAndView;
        }

    }
}